from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time
import sys
import os
import json

def create_output_dir():
    # 创建out目录
    out_dir = 'out'
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    return out_dir

def scrape_bing_images(keyword):
    # 配置Chrome选项
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')

    # 初始化浏览器
    driver = webdriver.Chrome(
        service=Service(ChromeDriverManager().install()),
        options=chrome_options
    )

    try:
        # 访问必应图片搜索
        url = f'https://cn.bing.com/images/search?q={keyword}'
        print(f'Accessing URL: {url}')
        driver.get(url)
        
        # 等待页面加载
        time.sleep(3)
        
        # 滚动加载更多图片
        print('Scrolling to load more images...')
        for _ in range(5):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(1.5)
        
        # 获取图片元素
        print('Extracting image URLs...')
        images = driver.find_elements(By.CSS_SELECTOR, 'div.imgpt a.iusc')
        image_urls = set()
        
        # 提取大图URL
        for img in images:
            try:
                # 获取包含图片信息的JSON
                m = img.get_attribute('m')
                if m:
                    img_info = json.loads(m)
                    # 获取原始大图URL
                    src = img_info.get('murl')
                    if src and src.startswith('http'):
                        # 移除URL中的尺寸参数
                        src = src.split('?')[0]
                        image_urls.add(src)
            except Exception as e:
                print(f'Error processing image: {e}')
                print(f'Image element: {img.get_attribute("outerHTML")}')
                continue
                
        return list(image_urls)
    
    except Exception as e:
        print(f'Error occurred: {e}')
        return []
    finally:
        driver.quit()

def save_urls_to_file(urls, filename):
    try:
        with open(filename, 'w', encoding='utf-8') as f:
            for url in urls:
                f.write(url + '\n')
        print(f'Saved {len(urls)} URLs to {filename}')
    except Exception as e:
        print(f'Error saving file: {e}')

def process_keywords_from_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            keywords = [line.strip() for line in f if line.strip()]
        return keywords
    except Exception as e:
        print(f'Error reading keywords file: {e}')
        return []

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Usage: python bing_image_scraper.py <keywords_file>')
        sys.exit(1)
        
    keywords_file = sys.argv[1]
    out_dir = create_output_dir()
    
    keywords = process_keywords_from_file(keywords_file)
    if not keywords:
        print('No valid keywords found in file')
        sys.exit(1)
        
    for keyword in keywords:
        output_file = os.path.join(out_dir, f'{keyword}_bing_image_urls.txt')
        print(f'Searching for images with keyword: {keyword}')
        image_urls = scrape_bing_images(keyword)
        
        if image_urls:
            save_urls_to_file(image_urls, output_file)
        else:
            print(f'No image URLs found for keyword: {keyword}')
